//
//  ByViewMineHeader.m
//  chequanbao_ios
//
//  Created by mc on 2017/5/8.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewMineHeader.h"

// vc
#import "ByUIMineDataVC.h"
@implementation ByViewMineHeader
{
    
    UIImageView *_avatarImg;
    UILabel     *_nickNameLabel;
    UIImageView *_editImg;
    
    UIImageView *_pointImg;
    UILabel     *_pointLabel;

}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)createViews{
    _avatarImg = [[UIImageView alloc] init];
    UITapGestureRecognizer *imgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    _avatarImg.userInteractionEnabled = YES;
    [_avatarImg addGestureRecognizer:imgTap];
    [self addSubview:_avatarImg];
    
    _nickNameLabel = [[UILabel alloc] init];
    _nickNameLabel.font = FONT(14);
    _nickNameLabel.textAlignment = NSTextAlignmentCenter;
    _nickNameLabel.textColor = [UIColor whiteColor];
    [self addSubview:_nickNameLabel];
    
    _editImg = [[UIImageView alloc] init];
    _editImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_editImg addGestureRecognizer:tap];
    _editImg.image = [UIImage imageNamed:@"mine_edit"];
    [self addSubview:_editImg];
    
    _pointImg = [[UIImageView alloc] init];
    _pointImg.image = [UIImage imageNamed:@"mine_small_points"];
    [self addSubview:_pointImg];
    
    _pointLabel = [[UILabel alloc] init];
    _pointLabel.font = FONT(14);
    _pointLabel.textColor = [UIColor whiteColor];
    [self addSubview:_pointLabel];
}

-(void)setUserModel:(ByModelUser *)userModel{
    _userModel = userModel;
    // 头像
    CGFloat x,y,width,height;
    x = (SCREEN_WIDTH - 80)/2;
//    if (@available(iOS 11.0,*)) {
//        y = self.viewController.view.safeAreaInsets.top;
//    }else{
//        y = 10 + 64;
//    }
    y = 10;
    width = height = 80;
    _avatarImg.layer.cornerRadius = height/2;
    _avatarImg.layer.masksToBounds = YES;
    _avatarImg.frame = CGRectMake(x, y, width, height);
    
    // 昵称
    width = [_userModel.nickname getSizeOfString:14].width;
    height = 20;
    x = (SCREEN_WIDTH - width - 20 - 5)/2;
    y = _avatarImg.bottom + 10;
    _nickNameLabel.frame = CGRectMake(x, y, width, height);
    
    // 编辑图标
    x = _nickNameLabel.right + 5;
    width = height = 20;
    _editImg.frame = CGRectMake(x, y, width, height);
    
    // 积分
    NSString *pointStr;
    if (_userModel.points) {
        pointStr = [NSString stringWithFormat:@"%.2f元",[_userModel.points floatValue]/100.0];
    }else{
        pointStr = @"0";
    }
    NSString *str = [NSString stringWithFormat:@"我的积分：%@",pointStr];
    width = [str getSizeOfString:14].width;
    x = (SCREEN_WIDTH - width - 20 - 5)/2  + 25;
    y = _nickNameLabel.bottom + 10;
    height = 20;
    _pointLabel.frame = CGRectMake(x, y, width, height);
    
    // 积分图标
    x = x - 25;
    width = height = 20;
    _pointImg.frame = CGRectMake(x, y, width, height);
    
    
    // 我的测试数据
    _nickNameLabel.text = _userModel.nickname;
    [_avatarImg setImageWithURL:[NSURL URLWithString:_userModel.head] placeholderImage:DEFAULT_IMAGE completed:nil];
    _pointLabel.text = str;
    
}

#pragma mark -- 编辑
- (void)tapAction:(UITapGestureRecognizer *)tap{
    if (!BY_USER) {
        ByUILoginVC *vc = [[ByUILoginVC alloc] init];
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
        return;
    }
    ByUIMineDataVC *vc = [[ByUIMineDataVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

@end
